<?php

namespace app\controller;

use app\BaseController;
use app\service\LoginService;
use app\service\NetworkBussinessService;
use app\service\NetworksService;
use app\service\TaskService;
use app\service\WarehouseService;
use app\utils\CaptchaUtils;
use app\validate\LoginValidate;
use think\exception\ValidateException;
use \think\facade\Request;
use think\captcha\facade\Captcha;
use think\facade\Config;

/**
 * 任务
 */
class Task extends BaseController
{


    /**
     * 任务列表
     * @return void
     */
    public function lists()
    {

        $uid = Request::param('uid/d', 0);
        $param['uid'] = $uid;
        $param['business_id'] = Request::param('business_id/d', 0);
        $param['name'] = Request::param('name/s', '');
        $param['page'] = Request::param('page/d', 0);
        $param['limit'] = Request::param('limit/d', 10);
        $param['no'] = Request::param('no/s', '');
        $param['task_status'] = Request::param('task_status/d', 0);
        $param['stime'] = Request::param('stime/s', '');
        $param['is_export'] = Request::param('is_export/d', 0);
        $param['etime'] = Request::param('etime/s', '');
        $param['car_id'] = Request::param('car_id/d', 0);
        $param['line_no'] = Request::param('line_no/s', '');
        $param['company_name'] = Request::param('company_name/s', '');
        $param['driver_name'] = Request::param('driver_name/s', '');
        $param['car_no'] = Request::param('car_no/s', '');

        //1 今天  2 历史
        $param['task_date'] = Request::param('task_date/d', 1);

        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $logParam['ip'] = Request::ip();
        $data = TaskService::lists($param);
        if ($data) {
            returnSuccess(200, '获取成功', $data);
        } else {
            returnFail(21101, '获取失败');
        }

    }


    /**
     * 管理员修改任务备注
     */
    public function modify_remarks()
    {
        $uid = Request::param('uid/d', 0);
        $param['id'] = Request::param('id/d', 0);
        $param['task_remarks'] = Request::param('task_remarks/s', '');
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['ip'] = Request::ip();

        $data = TaskService::modifyRemarks($param);
        if ($data) {
            returnSuccess(200, '更新成功');
        } else {
            returnFail(21101, '更新失败');
        }
    }


    public function review_abnormal()
    {
        $uid = Request::param('uid/d', 0);
        $param['id'] = Request::param('id/d', 0);
        $param['status'] = Request::param('status/0', 0);
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['ip'] = Request::ip();

        $data = TaskService::modifyTaskStatus($param);
        if ($data) {
            returnSuccess(200, '审核成功');
        } else {
            returnFail(21101, '审核失败');
        }
    }

    /**
     * 按月份导出数据
     * @return void
     */
    public function exportByMonth()
    {


        $param['stime'] = Request::param('stime/s', '');
        $uid = Request::param('uid/d', 0);
        $param['etime'] = Request::param('etime/s', '');
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);

        $logParam['ip'] = Request::ip();

        $data = TaskService::exportByMonth($param);
        returnSuccess(200, '获取成功', $data);

    }

    public function modify_driver()
    {
        $uid = Request::param('uid/d', 0);
        $param['id'] = Request::param('id/d', 0);
        $param['driver_id'] = Request::param('driver_id/d', 0);
        $param['car_id'] = Request::param('car_id/d', 0);
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);

        $param['ip'] = Request::ip();

        $data = TaskService::modify_driver($param);
        if ($data) {
            returnSuccess(200, '更新成功');
        } else {

            returnFail(21101, '更新失败');
        }
    }

    /**
     * @desc 任务详情
     * @return void
     */
    public function detail()
    {
        $uid = Request::param('uid/d', 0);
        $param['partner_id'] = get_partner_id($uid);
        $param['task_id'] = Request::param('task_id/d', 0);
        $info = TaskService::detail($param);
        returnSuccess(200, '获取成功', $info);

    }

    public function details()
    {
        $uid = Request::param('uid/d', 0);
        $param['partner_id'] = get_partner_id($uid);
        $param['task_id'] = Request::param('task_id/d', 0);
        $param['line_id'] = Request::param('line_id/d', 0);
        $info = TaskService::details($param);
        returnSuccess(200, '获取成功', $info);
    }

    //配送详情
    public function distribution()
    {
        $uid = Request::param('uid/d', 0);
        $param['partner_id'] = get_partner_id($uid);
        $param['page'] = Request::param('page/d', 0);
        $param['limit'] = Request::param('limit/d', 10);
        $param['uid'] = $uid;
        $param['is_export'] = Request::param('is_export/d', 0);
        $info = TaskService::distributionList($param);
        returnSuccess(200, '获取成功', $info);
    }


    //地图任务详情
    public function mapInfo()
    {
        $uid = Request::param('uid/d', 0);
        $param['task_id'] = Request::param('task_id/d', 0);
        $param['partner_id'] = get_partner_id($uid);
        if (empty($param['task_id'])) returnFail(21001, '参数错误');
        $data = TaskService::mapInfo($param);
        if ($data) {
            returnSuccess(200, '获取成功', $data);
        } else {

            returnFail(21101, '获取失败');
        }
    }

    //线路列表
    public function lineList()
    {
        $uid = Request::param('uid/d', 0);
        $param['partner_id'] = get_partner_id($uid);
        $param['now_time'] = "2022-07-07";//getTodayDate();
        $data = TaskService::lineList($param);
        returnSuccess(200, '获取成功', $data);
    }

    //路线详情
    public function routeDetails()
    {
        $uid = Request::param('uid/d', 0);
        $param['task_id'] = Request::param('task_id/d', 0);
        $param['partner_id'] = get_partner_id($uid);
        $param['limit'] = Request::param('limit/d', 10);
        $param['page'] = Request::param('page/d', 0);
        if (empty($param['task_id'])) returnFail(21001, '参数错误');
        $data = TaskService::historyRouteDetails($param);
        if ($data) {
            returnSuccess(200, '获取成功', $data);
        } else {
            returnFail(21101, '获取失败');
        }
    }

    //运行轨迹
    public function runTrack()
    {
        $param['car_id'] = Request::param('car_id/d', 0);
        $param['stime'] = Request::param('stime/s', '');
        $param['etime'] = Request::param('etime/s', '');
        if (empty($param['stime']) || empty($param['etime']) || empty($param['car_id'])) {
            returnFail(21007, '请完善选择条件哦');
        }
        $data = TaskService::getTrack($param);
        // dump($data);die;
        if (is_numeric($data)) {
            returnFail(25001, '系统数据错误');
        }
        if ($data) {
            returnSuccess(200, '获取成功', $data);
        } else {
            returnFail(21101, '获取失败');
        }
    }


}
